package com.hpeu_01.AtmApp;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;

public class Atm {
    private static double balance = 10000; //余额
    private static String user = "yll";//账号
    private static String password = "123";//密码
    private static Scanner scanner = new Scanner(System.in);
    private static String user01="qwer";//对方账号
    private static double balance01 = 100;//对方余额

    public static void show() {
        System.out.println("---------------------");
        System.out.println("#####   欢迎来到实训银行   #####");
        System.out.println("##### 请按照功能菜单进行操作 #####");
        System.out.println("---------------------");
    }
    //登录方法
    public static void login() {
        boolean x = true;
        int i = 5;
        while (x) {
            LocalDateTime dateTime = LocalDateTime.now();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
            String format = dateTimeFormatter.format(dateTime);
            System.out.println("请输入你的账号：");
            String zhanghao = scanner.nextLine();
            System.out.println("请输入你的密码：");
            String mima = scanner.nextLine();
            if (zhanghao.equals(user) && mima.equals(password)) {
                x = false;
                System.out.println("登录成功！当前余额：" + balance + "登录时间：" + format);
            } else {
                i--;
                System.out.println("账号或者密码错误，请重新输入，你还有" + i + "次机会");
            }
            if (i == 0) {
                System.out.println("密码已经错误五次，请联系银行工作人员。");
                System.exit(0);
            }
        }
    }

    //余额
    public static double balance(){
        return balance;
    }
    //存款的方法
    public static double deposit(){
        System.out.println("请输入存入的金额：");
        double v = scanner.nextDouble();
        if (v%100==0){
            balance+=v;
            System.out.println("当前余额为："+balance);
        }else {
            System.out.println("存入的金额需是100的整数");
        }
        return balance;
    }

    //取款Withdrawal
    public static double withdrawal(){
        System.out.println("请输入你要取款的金额：");
        double s = scanner.nextDouble();
        if (s%100==0){
            if (balance>=s){
                balance-=s;
                System.out.println("当前余额为："+balance);
            }else {
                System.out.println("你的余额不足！");
            }
        }else {
            System.out.println("取款的金额需是100的整数");
        }
        return balance;
    }

    //转账的方法
    public static double transfer(){
        System.out.println("请输入对方的账号：");
        String user02 = scanner.nextLine();
        if (user02.equals(user01)){
            System.out.println("请输入转账的金额：");
            double r = scanner.nextDouble();
            if (balance>=r){
                balance-=r;
                balance01+=r;
                System.out.println("转账成功，你当前的账户余额为:"+balance+" 对方的账户余额为："+balance01);
            }else {
                System.out.println("余额不足!");
            }
        }else {
            System.out.println("该账户不存在!");
        }
        return balance;
    }

    //改密码
    public static String changePassword(){
        System.out.println("请输入当前密码：");
        String p = scanner.nextLine();
        if (p.equals(password)){
            System.out.println("请输入新密码：");
            String p1 = scanner.nextLine();
            password=p1;
            System.out.println("密码修改成功！请重新登录！");
            login();
        }else {
            System.out.println("你输入的密码错误！");
        }
        return password;
    }

    //退出
    public static int dropout(){
        int i=0;
        System.out.println("你确定要退出系统？是则按Y，否则按N");
        char c = scanner.nextLine().toUpperCase().charAt(0);
        if (c=='Y'||c=='N'){
            if (c=='Y'){
                System.out.println("你已退出系统，是否再次登录，是则按Y，否则按N");
                char c1 = scanner.nextLine().toUpperCase().charAt(0);
                if (c1=='Y'){
                    login();
                    return i=2;
                }else if (c1=='N'){
                    System.out.println("你已退出系统!");
                    return i=1;
                }
            }
        }else {
            System.out.println("输入错误！");
        }
        return i;
    }
}
